//
//  FBProgressView.m
//  FingerBuinessOC
//
//  Created by 杨琴 on 16/8/5.
//  Copyright © 2016年 yangqin. All rights reserved.
//

#import "FBProgressView.h"
#import <Masonry/Masonry.h>

#define kContentViewHeight   110
#define kContentViewWidth    150
#define kMessageLabelHeight  20
#define kMargin  10

@interface FBProgressView ()

@property (nonatomic, strong) UIView * contentView;
@property (nonatomic, strong) CAReplicatorLayer * replicatorLayer;

@end

@implementation FBProgressView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupSubViews];
    }
    return self;
}

+ (void)showWithMessage:(NSString *)meaasge {
    
    FBProgressView *progressView = [[FBProgressView alloc] init];
    progressView.frame = [UIScreen mainScreen].bounds;
    
    if (meaasge != nil) {
        progressView.meaasgeLabel.text = meaasge;
    }
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    [keyWindow addSubview:progressView];
    
}


+ (void)hide {

    NSArray *subViews = [UIApplication sharedApplication].keyWindow.subviews;
   
    for (UIView *view in subViews) {
        if ([view isKindOfClass:[FBProgressView class]]) {
            [view removeFromSuperview];
            break;
        }
    }
}

- (void)setupSubViews {
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.meaasgeLabel];
    [self.contentView.layer addSublayer:self.replicatorLayer];
    
    
    __weak typeof(self) mySelf = self;
    // 添加约束
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kContentViewWidth);
        make.height.mas_equalTo(kContentViewHeight);
        make.center.equalTo(mySelf);
    }];
    
    [self.meaasgeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(kMessageLabelHeight);
        make.bottom.mas_equalTo(-kMargin);
        
    }];
}

#pragma mark - Getter 

- (UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.layer.cornerRadius = 10;
        _contentView.layer.masksToBounds = YES;
        _contentView.backgroundColor = [UIColor grayColor];
    }
    
    return _contentView;
}

- (UILabel *)meaasgeLabel {
    if (_meaasgeLabel == nil) {
        _meaasgeLabel = [[UILabel alloc] init];
        _meaasgeLabel.textColor = [UIColor whiteColor];
        _meaasgeLabel.textAlignment = NSTextAlignmentCenter;
        _meaasgeLabel.text = @"loading...";
    }
    return _meaasgeLabel;
}

- (CAReplicatorLayer *)replicatorLayer {
    if (_replicatorLayer == nil) {
        _replicatorLayer = [CAReplicatorLayer layer];
        
        CGFloat height = kContentViewHeight - 3 * kMargin - kMessageLabelHeight;
        height = MIN(height, kContentViewWidth - 2 * kMargin);
        
        _replicatorLayer.bounds = CGRectMake(0, 0, height, height);
        _replicatorLayer.position = CGPointMake(kContentViewWidth / 2, kMargin + height / 2);
        
        CALayer *dotLayer = [CALayer layer];
        dotLayer.bounds = CGRectMake(0, 0, 10, 10);
        dotLayer.position = CGPointMake(height / 2, 10 / 2);
        dotLayer.cornerRadius = 10 / 2;
        dotLayer.backgroundColor = [UIColor whiteColor].CGColor;
        dotLayer.transform = CATransform3DMakeScale(0, 0, 0);
        
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        animation.duration = 1;
        animation.fromValue = @1.0;
        animation.toValue = @0;
        animation.repeatCount = CGFLOAT_MAX;
        [dotLayer addAnimation:animation forKey:nil];
        
        [_replicatorLayer addSublayer:dotLayer];
        
        NSInteger count = 10;
        _replicatorLayer.instanceCount = count;
        
        CGFloat angle = (2 * M_PI) / (CGFloat) count;
        
        _replicatorLayer.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1);
        _replicatorLayer.instanceDelay = animation.duration / count;
        
    }
    
    return _replicatorLayer;
}

@end
